package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址簿功能
 * @author ChuMingYu
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @author ChuMingYu
     */
    @PostMapping
    public R saveAddressBook(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 设置默认地址
     * @author ChuMingYu
     */
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        addressBookLambdaUpdateWrapper.eq(AddressBook::getUserId,addressBook.getUserId());
        addressBookService.update(addressBookLambdaUpdateWrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据ID查询地址
     * @author ChuMingYu
     */
    @GetMapping("/{id}")
    public R getAddressById(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        }else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     * @author ChuMingYu
     */
    @GetMapping("/default")
    public R getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(qw);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询指定用户的全部地址
     * @author ChuMingYu
     */
    @GetMapping("/list")
    public R getAddressById(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(addressBook.getUserId()!= null,AddressBook::getUserId,addressBook.getUserId());
        qw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);

    }

    /**
     * 删除地址【可能是多个】
     * @author ChuMingYu
     */

    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.in(AddressBook::getUserId,ids);
        boolean remove = addressBookService.remove(qw);

        if (remove) {
            return R.success("删除成功");
        } else {
            return  R.error("删除失败");
        }
    }

    /**
     * 修改地址
     * @author ChuMingYu
     */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getId,addressBook.getId());
        boolean update = addressBookService.update(addressBook, qw);

        if (update) {
            return  R.success("修改成功");
        } else {
            return  R.error("修改失败");
        }
    }
}
